package com.muli.muwai.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muli.muwai.entity.IntfFinUserTrend;
import com.muli.muwai.mapper.UserTrendMapper;
import com.muli.muwai.service.UserTrendService;
import com.muli.muwai.util.DateUtils;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserTrendServiceImpl extends ServiceImpl<UserTrendMapper, IntfFinUserTrend> implements UserTrendService {
    @Override
    public void userTrend(List<String[]> userTrend) {
        List<IntfFinUserTrend> list = getIntfFinUserTrends(userTrend);
        //获取excel日期
        for (IntfFinUserTrend trend : list) {
            //获取日期
            Timestamp visitTime = trend.getVisitTime();
            QueryWrapper<IntfFinUserTrend> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("visit_time",visitTime);
            IntfFinUserTrend one = baseMapper.selectOne(queryWrapper);
            if(one!=null){
                trend.setId(one.getId());
            }
        }
        saveOrUpdateBatch(list);
    }

    private List<IntfFinUserTrend> getIntfFinUserTrends(List<String[]> userTrend) {
        List<IntfFinUserTrend> list = new ArrayList<>();
        for (String[] strings : userTrend) {
            IntfFinUserTrend entity = new IntfFinUserTrend();
            //entity.setVisitTime(strings[0]);
            try {
                Date date1 = DateUtils.parseString2Date(strings[0], "yy/MM/dd");
                Timestamp timestamp1 = new Timestamp(date1.getTime());
                entity.setVisitTime(timestamp1);
            } catch (Exception e) {
                entity.setVisitTime(null);
            }
            entity.setNewUser(strings[1]);
            entity.setActiveUser(strings[2]);
            entity.setOpenCount(strings[3]);
            entity.setPageView(strings[4]);
            //entity.setStayperiodPerView(strings[5]);
            //转化为秒
            Double number = Double.parseDouble(strings[5]);
            //四舍五入
            double ceil = Math.round(number*24*60*60);
            long seconds = new Double(ceil).longValue();
            String s = DateUtils.formatSeconds(seconds);
            entity.setStayperiodPerView(s);

            //entity.setStayperiodPerMan(strings[6]);
            Double number1 = Double.parseDouble(strings[6]);
            double ceil1 = Math.round(number1*24*60*60);
            long seconds1 = new Double(ceil1).longValue();
            String s1 = DateUtils.formatSeconds(seconds1);
            entity.setStayperiodPerMan(s1);

            entity.setStayperiodPerOpen(strings[7]);

            list.add(entity);
        }
        return list;
    }
}
